package cn.edu.heuet.bnve.fragment;

import androidx.annotation.RequiresApi;
import androidx.databinding.DataBindingUtil;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.xuexiang.xui.utils.ResUtils;
import com.xuexiang.xui.widget.button.SmoothCheckBox;
import com.xuexiang.xui.widget.tabbar.TabControlView;

import java.util.List;

import cn.edu.heuet.bnve.R;
import cn.edu.heuet.bnve.model.Schedule;
import cn.edu.heuet.bnve.activity.ScheduleActivity.ScheduleDetailActivity;
import cn.edu.heuet.bnve.adapter.ScheduleAdapterAll;
import cn.edu.heuet.bnve.base.fragment.BaseFragment;
import cn.edu.heuet.bnve.database.ScheduleDao;
import cn.edu.heuet.bnve.databinding.FragmentScheduleListBinding;
import cn.edu.heuet.bnve.view.group.GroupItemDecoration;
import cn.edu.heuet.bnve.view.group.GroupRecyclerView;
import cn.edu.heuet.bnve.view.RecyclerItemClickListener;

public class ScheduleListFragment extends BaseFragment {


    private FragmentScheduleListBinding binding;
    private GroupRecyclerView mRecyclerView;
    private ScheduleDao scheduleDao;
    private List<Schedule> schedules;

    private TabControlView mTabControlView;
    private  int selectTab;

    private final static  int NOT_COMPLETED=0;

    private final static  int COMPLETED=1;

    private final static  int ALL=2;

    private  int type;


    public static ScheduleListFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        ScheduleListFragment fragment = new ScheduleListFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_schedule_list;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void initView() {
        binding = DataBindingUtil.bind(mRootView);
        scheduleDao = new ScheduleDao(mContext);

        mTabControlView = binding.tcvSelect;
        initTabControlView();

        mRecyclerView = binding.scheduleList;
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.addItemDecoration(new GroupItemDecoration<String, Schedule>());

    }

    @Override
    protected void initData() {

        type = getArguments().getInt("type");
    }

    @Override
    protected void initEvent() {

        mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(mContext, new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if(schedules.get(position).getType()!=-1&&schedules.get(position).getType()!=2) {
                    Intent intent = new Intent(mContext, ScheduleDetailActivity.class);
                    intent.putExtra("schedule", schedules.get(position));
                    startActivity(intent);
                }
            }
            @Override
            public void onLongClick(View view, int posotion) {
                if(schedules.get(posotion).getType()==0||schedules.get(posotion).getType()==1){
                    SmoothCheckBox checkBox = view.findViewById(R.id.cb_complete);
                    Schedule schedule = schedules.get(posotion);
                    if(checkBox.isChecked()){
                        checkBox.setChecked(false,true);
                        schedule.setIsComplete(0);
                    }
                    else{
                        checkBox.setChecked(true,true);
                        schedule.setIsComplete(1);
                    }
                    scheduleDao.updateDataById(schedule.getId(),schedule);
                }
            }
        }));
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    private void initTabControlView() {
        try {
            mTabControlView.setItems(ResUtils.getStringArray(R.array.complete_param_option),new String[]{0+"",1+"",2+""});
            mTabControlView.setDefaultSelection(NOT_COMPLETED);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mTabControlView.setOnTabSelectionChangedListener((title, value) -> tabChange(Integer.parseInt(value)));
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onStart() {
        super.onStart();
        if(scheduleDao!=null) {
            reLoad(selectTab);
        }
    }


    /**
     * 0 全部 1已完成 2 未完成
     * @param value
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    private void tabChange(int value){
        reLoad(value);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void reLoad(int choice){
        selectTab = choice;
        if(choice!=ALL){
            schedules =scheduleDao.getAll(this.type,choice);
            ScheduleAdapterAll adapter = new ScheduleAdapterAll(mContext, schedules);
            schedules = adapter.getAllSchedules();
            mRecyclerView.setAdapter(adapter);
            mRecyclerView.notifyDataSetChanged();
            mRecyclerView.scrollToPosition(adapter.getItemCount()-1);
        }
        else{
            schedules =scheduleDao.getAll(this.type);
            ScheduleAdapterAll adapter = new ScheduleAdapterAll(mContext, schedules);
            schedules = adapter.getAllSchedules();
            mRecyclerView.setAdapter(adapter);
            mRecyclerView.notifyDataSetChanged();
            mRecyclerView.scrollToPosition(adapter.getItemCount()-1);
        }
    }




}